#include"Sock.hpp"
#include<unistd.h>
void Usage(string proc)
{
  cout <<"Usage\n\t" << proc << " server_ip" << " server_port " << endl;
  exit(1);
}
int main(int argc,char* argv[])
{
  if(argc != 3)
  {
    Usage(argv[0]);
  }
  string server_ip = argv[1];
  uint16_t server_port = atoi(argv[2]);
  int sock = Sock::Socket();
  cout <<"Socket Success !" << endl;
  
  Sock::Connect(sock,server_ip,server_port);
  cout <<"Connect Success !" << endl;
  for(;;)
  {
    //从键盘获取用户输入,然后发送给服务端, 然后接收服务端的响应
    cout <<"Please Enter:" ;
    char cmd[1024] = {0};
    fgets(cmd,sizeof(cmd)-1,stdin); //注意这里的第三个参数不是文件描述符，而是FILE* 类型的文件指针
    //注意:从键盘当中读取还会读到\n  即:假设输入hello 实际为:hello\n\0
    cmd[strlen(cmd)-1] = '\0'; //将\n置为\0
    
    write(sock,cmd,sizeof(cmd)-1); // 写入的时候不写\0
    
    ssize_t n = read(sock,cmd,sizeof(cmd)-1);
    cmd[n] = 0;
    cout <<"server# " << cmd << endl;
  }
  return 0;

